//
//  StudentBaseViewController.m
//  Eath
//
//  Created by mao on 10/28/14.
//  Copyright (c) 2014 yuhaya. All rights reserved.
//

#import "StudentBaseViewController.h"
#import "StudentAddViewController.h"
#import "Tool.h"

@interface StudentBaseViewController ()

@end

@implementation StudentBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"学生信息";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(stuEdit)];
    // Do any additional setup after loading the view from its nib.
    CGRect tableFrame = CGRectMake(0, 0, 320, [Tool tableHeight] - 20);
    self.table_msg = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped];
    self.table_msg.delegate = self;
    self.table_msg.dataSource = self;
    [self.view addSubview:self.table_msg];
    
    self.school_id = self.stu.school_id;
    self.identify = self.stu.identify;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateStu:) name:STUS_CHANGE object:nil];
}

- (void) updateStu:(NSNotification*)notification
{
    StudentList * stu_list = [StudentList shareStudentList];
    self.stu = [stu_list getStudentByIdentify:self.identify];
    [self.table_msg reloadData];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * Cell = [[UITableViewCell alloc] init];
    
    if (indexPath.section == 0 && indexPath.row == 0) {
        
        UIImageView * photo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.stu.photo]];
        photo.frame = CGRectMake(20, 20, 60, 60);
        [Cell.contentView addSubview:photo];
        
    }else{
        
        CGRect label_fram = CGRectMake(20, 0, 90, 35);
        UILabel * label = [[UILabel alloc] initWithFrame:label_fram];
        label.font = [UIFont systemFontOfSize:13];
        
        CGRect label_val_fram = CGRectMake(110, 0, 200, 35);
        UILabel * label_val = [[UILabel alloc] initWithFrame:label_val_fram];
        label_val.font = [UIFont systemFontOfSize:13];
        
        if (indexPath.section == 0) {
            
            switch (indexPath.row) {
                case 1:
                    label.text = @"姓名";
                    label_val.text = self.stu.name;
                    break;
                    
                case 2:
                    label.text = @"性别";
                    label_val.text = self.stu.sex ? @"女" : @"男";
                    break;
                    
                default:
                    break;
            }
        }
        
        if (indexPath.section == 1) {
            
            switch (indexPath.row) {
                case 0:
                    label.text = @"卡号";
                    label_val.text = self.stu.card;
                    break;
                    
                case 1:
                    label.text = @"所属学校";
                    label_val.text = self.stu.school_name;
                    break;
                    
                case 2:
                    label.text = @"所属年级";
                    label_val.text = self.stu.grade;
                    break;
                    
                case 3:
                    label.text = @"所属班级";
                    label_val.text = self.stu.class_name;
                    break;
                    
                case 4:
                    label.text = @"入学时间";
                    label_val.text = self.stu.enrol_time;
                    break;
                    
                case 5:
                    label.text = @"亲属关系";
                    label_val.text = self.stu.relation;
                    break;
            }
        }
        [Cell.contentView addSubview:label];
        [Cell.contentView addSubview:label_val];
    }
    
    return Cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }else{
        return 6;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 100;
    }else{
        return 35;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (void)stuEdit
{
    StudentAddViewController * edit_stu = [[StudentAddViewController alloc] init];
    edit_stu.stu = self.stu;
    edit_stu.stu_base = self;
    DLog(@"%@",self.stu);
    [self.navigationController pushViewController:edit_stu animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
//    NSString * student_id = [self.stu identify];
//    DLog(@"%@",student_id);
    
    [self.table_msg reloadData];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
